package character.sprite.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;

import character.sprite.data.CharacterStyleDatabase;

public class CharacterStyleListPanel extends JPanel {

	private static final long serialVersionUID = -8061545298455482117L;

	private JTabbedPane styleTabs;
	private HashMap<String, CharacterStyleList> styleLists;

	public CharacterStyleListPanel() {
		this.setLayout(new BorderLayout());

		styleTabs = new JTabbedPane();
		styleTabs.setPreferredSize(new Dimension(200, 0));
		styleLists = new HashMap<String, CharacterStyleList>();
		styleLists.put("Body", new CharacterStyleList());
		styleLists.put("Hair", new CharacterStyleList());
		styleLists.put("Head", new CharacterStyleList());
		styleLists.put("Face", new CharacterStyleList());
		styleLists.put("Dress", new CharacterStyleList());

		for (String s : styleLists.keySet()) {
			JScrollPane scroll = new JScrollPane(styleLists.get(s));
			scroll
					.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			scroll
					.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
			styleTabs.addTab(s, scroll);
		}

		this.updateListsFromDatabase();

		this.add(styleTabs, BorderLayout.CENTER);
	}

	private void updateListsFromDatabase() {
		String[] bodies = new String[CharacterStyleDatabase.getInstance()
				.getAvailableBodies().size()];
		CharacterStyleDatabase.getInstance().getAvailableBodies().toArray(
				bodies);
		styleLists.get("Body").setListData(
				CharacterStyleDatabase.getInstance().toStyleArray("Body",
						bodies));
		String[] hair = new String[CharacterStyleDatabase.getInstance()
				.getAvailableHair().size()];
		CharacterStyleDatabase.getInstance().getAvailableHair().toArray(hair);
		styleLists.get("Hair")
				.setListData(
						CharacterStyleDatabase.getInstance().toStyleArray(
								"Hair", hair));
		String[] head = new String[CharacterStyleDatabase.getInstance()
				.getAvailableHeads().size()];
		CharacterStyleDatabase.getInstance().getAvailableHeads().toArray(head);
		styleLists.get("Head")
				.setListData(
						CharacterStyleDatabase.getInstance().toStyleArray(
								"Head", head));
		String[] face = new String[CharacterStyleDatabase.getInstance()
				.getAvailableFaces().size()];
		CharacterStyleDatabase.getInstance().getAvailableFaces().toArray(face);
		styleLists.get("Face")
				.setListData(
						CharacterStyleDatabase.getInstance().toStyleArray(
								"Face", face));
		String[] dress = new String[CharacterStyleDatabase.getInstance()
				.getAvailableDresses().size()];
		CharacterStyleDatabase.getInstance().getAvailableDresses().toArray(
				dress);
		styleLists.get("Dress").setListData(
				CharacterStyleDatabase.getInstance().toStyleArray("Dress",
						dress));
	}

}
